package com.example.framework;

import org.openqa.selenium.By;
import com.example.tests.ContactObject;

public class ContactHelper extends HelperWithWebDriverBase {

	public ContactHelper (ApplicationManager manager) {
		super(manager);
	}	
	public void createContact(ContactObject validContact) {
		manager.getNavigationHelper().openMainPage();
		initContactCreation();
		fillContactFormWithGroup(validContact);
		submitElementCreation();
		returnToHomePage();
	}

	private void fillContactFormWithGroup(ContactObject contactObject) {
		fillContactFormWithoutGroup(contactObject);
		fillDropDownList(By.name("new_group"), contactObject.groupMembership);
	}
	
	private void fillContactFormWithoutGroup(ContactObject contactObject) {
		type(By.name("firstname"),contactObject.firstName);
		type(By.name("lastname"),contactObject.lastName);
		type(By.name("address"),contactObject.address);
		type(By.name("home"),contactObject.homePhone);
		type(By.name("mobile"),contactObject.mobilePhone);
		type(By.name("work"),contactObject.workPhone);
		type(By.name("email"),contactObject.email);
		type(By.name("email2"),contactObject.email2);
		fillDropDownList(By.name("bday"), contactObject.birthDay);
		fillDropDownList(By.name("bmonth"), contactObject.birthMonth);
		type(By.name("byear"),contactObject.birthYear);
		type(By.name("address2"),contactObject.address2);
		type(By.name("phone2"),contactObject.homePhone2);
	}
	
	private void returnToHomePage() {
		click(By.linkText("home page"));
	}

	private void initContactCreation() {
		click(By.linkText("add new"));
	}
	public void modifyContact(int i, ContactObject modifiedContact) {
		manager.getNavigationHelper().openMainPage();
		initContactModification(i);
		fillContactFormWithoutGroup(modifiedContact);
		submitContactModification();
	}
	private void submitContactModification() {
		click(By.name("update"));
	}
	private void initContactModification(int i) {
		click(By.cssSelector("img[alt=\"Edit\"]"));
	}
	public void deleteContact(int i) {
		manager.getNavigationHelper().openMainPage();
		initContactModification(i);
		submitContactRemoval();
	}
	private void submitContactRemoval() {
		click(By.xpath("(//input[@name='update'])[2]"));
	}
	
}
